const app = getApp();
import cfg from "../utils/config.js";
import { get, post, getArrDifference } from "../utils/util.js";

//获取下一个景点
export const getNextPoint = () => {
	let addressList = uni.getStorageSync("addressList");
	let ids = addressList.map(d => d.id);
	let finshList = uni.getStorageSync("finshList");
	let diffArr = getArrDifference(ids, finshList);
	return diffArr;
};

const videoConfig = {
	1: "https://file-oss.putaocdn.com/minhangwenchuang/video/1.mp3",
	2: "https://file-oss.putaocdn.com/minhangwenchuang/video/2.mp3",
	3: "https://file-oss.putaocdn.com/minhangwenchuang/video/3.mp3",
	4: "https://file-oss.putaocdn.com/minhangwenchuang/video/4.mp3",
	5: "https://file-oss.putaocdn.com/minhangwenchuang/video/5.mp3",
	6: "https://file-oss.putaocdn.com/minhangwenchuang/video/6.mp3",
	7: "https://file-oss.putaocdn.com/minhangwenchuang/video/7.mp3",
	8: "https://file-oss.putaocdn.com/minhangwenchuang/video/8.mp3",
	9: "https://file-oss.putaocdn.com/minhangwenchuang/video/9.mp3",
	bg: "https://file-oss.putaocdn.com/minhangwenchuang/video/bg.mp3",
	cg: "https://file-oss.putaocdn.com/minhangwenchuang/video/cg.mp3",
	xz: "https://file-oss.putaocdn.com/minhangwenchuang/video/xz.mp3"
};

//播放音频
let innerAudio;
export const playMp3 = config => {
	if (config.isNew) {
		let newInnerAudio = uni.createInnerAudioContext();
		newInnerAudio.autoplay = true;
		newInnerAudio.src = videoConfig[config.type];
		newInnerAudio.loop = config.loop || false;
	} else {
		if (!innerAudio) {
			innerAudio = uni.createInnerAudioContext();
		}
		innerAudio.autoplay = true;
		innerAudio.src = videoConfig[config.type];
		innerAudio.loop = config.loop || false;
	}
};
export const endMp3 = config => {
	if(innerAudio){
		innerAudio.destroy();
		innerAudio =  null
	}
};
let innerBg;
export const playMpBg = config => {
	innerBg = uni.createInnerAudioContext();
	innerBg.autoplay = true;
	innerBg.src = videoConfig["bg"];
	innerBg.loop = true;
};
export const endMpBg = config => {
	if(innerBg){
		innerBg.destroy();
	}

};

export const getAuthorization = config => {
	uni.getSetting({
		success(res) {
			if (!res.authSetting[config.type]) {
				uni.authorize({
					scope: config.type,
					success() {
						config.callBack();
					},
					fail() {
						uni.showModal({
							title: "授权",
							content: `需要获取您的${config.tip}权限,请到小程序的设置中打开授权`,
							confirmText: "设置",
							confirmColor: "#317EF3",
							success(res) {
								if (res.confirm) {
									uni.openSetting({
										success(res) {
											if (res.authSetting[config.type]) {
												config.callBack();
											}
										}
									});
								}
							}
						});
					}
				});
			} else {
				config.callBack();
			}
		}
	});
};
